import requst from '../../../util/Http.js'
Page({                                                  // page项
  data: {
    checkboxVal:'',
    bool:false,
    url: '',
    name: '324',
  },
  getname(e) {
    let name = e.detail.value;
    if (name.length === 0) return;
    console.log(e)
    this.setData({
      name
    })
  },

  checkboxChange(e){
    let bool=false;
    e.detail.value.length?bool=true:bool=false;
    this.setData({
      bool
    })
  },

  chooseavatar(e){
    const {
      avatarUrl
     } = e.detail
    this.geturl(avatarUrl)
  },

   login(){
     let than=this;
    if(!this.data.bool){
      wx.showToast({
        title: '请先同意隐私协议',
      })
      return
    }
    if(this.data.name.trim().length==0){
      wx.showToast({
        title: '请输入呢称',
      })
      return
    }
    if(this.data.url.length==0){
      wx.showToast({
        title: '请上传头像',
      })
      return
    }
    this.setData({
      ztBool:true
    })
    let {name,url}=this.data
    wx.login({
      success: (res) => {
        requst('Index/getUserLogin',{code:res.code,name,url},'post').then(rese=>{
          than.setData({
            ztBool:false
          })
          wx.setStorageSync('token',rese.data.token)
          wx.setStorageSync('userinfo',rese.data.userinfo)
          wx.switchTab({
            url: '/page/component/index/index',
          });
        }).catch(err=>{
          console.log(err)
        })
      },
      fail(err){
          console.log(err)
      }
    })
    
  },

  geturl(newurl){
    let than=this;
    wx.uploadFile({
      filePath:newurl,
      name: 'file',
      url: 'http://127.0.0.1:8080/wxapp/Index/updateqiyun',
      success(res){
          let url=JSON.parse(res.data).data;
          than.setData({
            url
          })
      },
      fail(err){
        console.log(err)
      }
    })
  }

})